/**
 * 
 */
package ro.dta.idbi.ui;

import java.awt.BasicStroke;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.plaf.basic.BasicButtonUI;

/**
 * Closeable tab used on the right-hand side of the main application
 * 
 * @see <code>http://download.oracle.com/javase/tutorial/uiswing/examples/components/
 *      TabComponentsDemoProject/src/components/ButtonTabComponent.java</code>
 * 
 */
public class CloseableTab extends JPanel {

	private static final long serialVersionUID = 3348878355659411533L;

	/**
	 * Icon for the close button associated to the tab, in normal state
	 */
	private static final ImageIcon CLOSE_NORMAL = new ImageIcon("./resources/close.png");

	/**
	 * Icon for the close button associated to the tab, in hover state
	 */
	private static final ImageIcon CLOSE_HOVER = new ImageIcon("./resources/close_press.png");

	/**
	 * Parent tabbed pane holding the tab
	 */
	private final transient JTabbedPane parentPane;

	/**
	 * CloseableTab class constructor
	 * 
	 * @param parentPane
	 *            Parent tabbed pane of this tab
	 */
	public CloseableTab(final JTabbedPane parentPane) {
		super();
		this.parentPane = parentPane;

		setOpaque(false);
		setLayout(new FlowLayout(FlowLayout.LEFT, 0, 3));

		final JLabel label = new JLabel() {
			private static final long serialVersionUID = 6823293405153353490L;

			@Override
			public String getText() {
				final int index = parentPane.indexOfTabComponent(CloseableTab.this);
				String result = null;
				if (index != -1) {
					result = parentPane.getTitleAt(index);
				}
				return result;
			}

			@Override
			public Icon getIcon() {
				final int index = parentPane.indexOfTabComponent(CloseableTab.this);
				Icon result = null;
				if (index != -1) {
					result = parentPane.getIconAt(index);
				}
				return result;
			}
		};

		add(label);

		final JButton closeBtn = new TabButton();
		closeBtn.setOpaque(false);
		add(closeBtn);
	}

	/**
	 * 
	 * @see <code>http 
	 *      ://download.oracle.com/javase/tutorial/uiswing/examples/components
	 *      /TabComponentsDemoProject/src/components/ButtonTabComponent.java</code>
	 * 
	 */
	private class TabButton extends JButton implements ActionListener {

		private static final long serialVersionUID = 7086551234793337251L;

		/**
		 * Size of the close tab button
		 */
		private static final int SIZE = 16;

		/**
		 * TabButton class constructor
		 */
		public TabButton() {
			super();

			setPreferredSize(new Dimension(SIZE, SIZE));
			setToolTipText("Close tab");
			setUI(new BasicButtonUI());
			setContentAreaFilled(false);
			setFocusable(false);
			setBorderPainted(false);
			setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			setRolloverEnabled(true);

			addActionListener(this);
		}

		@Override
		public void actionPerformed(final ActionEvent event) {
			final int index = parentPane.indexOfTabComponent(CloseableTab.this);
			if (index != -1) {
				parentPane.remove(index);
			}
		}

		@Override
		public void updateUI() {
			// Do nothing
		}

		@Override
		protected void paintComponent(final Graphics graphics) {
			super.paintComponent(graphics);
			final Graphics2D g2d = (Graphics2D) graphics.create();

			if (getModel().isPressed()) {
				g2d.translate(1, 1);
			}

			g2d.setStroke(new BasicStroke(2));
			if (getModel().isRollover()) {
				g2d.drawImage(CLOSE_HOVER.getImage(), 0, 0, null);
			} else {
				g2d.drawImage(CLOSE_NORMAL.getImage(), 0, 0, null);
			}

			g2d.dispose();
		}
	}
}
